define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'profit/index' + location.search,
                    add_url: 'profit/add',
                    edit_url: 'profit/edit',
                    del_url: 'profit/del',
                    multi_url: 'profit/multi',
                    import_url: 'profit/import',
                    table: 'profit',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'main_type', title: __('Main_type'), searchList: {"1":__('Main_type 1'),"2":__('Main_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'main_id', title: __('Main_id')},
                        {field: 'product.name', title: __('Product.name'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                name: 'setting',
                                text: __('分润设置'),
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'profit/setting'
                            }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            require(['bootstrap-select', 'bootstrap-select-lang']);
            var renderselect = function (select, data) {
                var html = [];
                for (var i = 0; i < data.length; i++) {
                    html.push("<option value='" + data[i].id + "'>" + data[i].name + "</option>");
                }
                $(select).html(html.join(""));
                select.trigger("change");
                if (select.data("selectpicker")) {
                    select.selectpicker('refresh');
                }
                return select;
            };

            $(document).on('change', "#c-policy", function () {
                let val = $(this).val();
                if(val > 0){
                    Fast.api.ajax({
                        url: 'profit/ajax',
                        data: {id: val},
                        loading: false
                    }, function (data) {
                        $("#c-product_id").val(data.product_id);
                        $("#c-product_id").selectPageRefresh();
                        Fast.api.ajax({
                            url: "profit/list",
                            data: {product_id: data.product_id,profit_id: val}
                        }, function (data) {
                            var rate = data.rate;
                            var card_type = data.card_type;
                            var tran = data.transaction_type;
                            var channels = data.channels;
                            $("#c-rate_ids,#c-card_type,#c-transaction_type_ids,#c-channel_ids").selectPageClear();
                            $("#c-rate_ids").selectPageData(rate);
                            $("#c-card_type").selectPageData(card_type);
                            $("#c-transaction_type_ids").selectPageData(tran);
                            $("#c-channel_ids").selectPageData(channels);
                            $("#c-rate_ids,#c-card_type,#c-transaction_type_ids,#c-channel_ids").selectPageRefresh();
                            // $("#relation-zone .relation-item").remove();
                            // renderselect($("#c-rate_ids"), rate);
                            // renderselect($("#c-card_type"), card_type);
                            // renderselect($("#c-transaction_type_ids"), tran);
                            // renderselect($("#c-channel_ids"), channels);
                            return false;
                        });
                        return false;
                    }, function () {
                        return false;
                    });
                }
            });

            $(document).on('change', "#c-product_id", function () {
                var product_id = $(this).val();
                Fast.api.ajax({
                    url: "profit/list",
                    data: {product_id: product_id},
                }, function (data, ret) {
                    var rate = data.rate;
                    var card_type = data.card_type;
                    var tran = data.transaction_type;
                    var channels = data.channels;
                    $("#c-rate_ids,#c-card_type,#c-transaction_type_ids,#c-channel_ids").selectPageClear();
                    $("#c-rate_ids").selectPageData(rate);
                    $("#c-card_type").selectPageData(card_type);
                    $("#c-transaction_type_ids").selectPageData(tran);
                    $("#c-channel_ids").selectPageData(channels);
                    $("#c-rate_ids,#c-card_type,#c-transaction_type_ids,#c-channel_ids").selectPageRefresh();
                    // $("#relation-zone .relation-item").remove();
                    // renderselect($("#c-rate_ids"), rate);
                    // renderselect($("#c-card_type"), card_type);
                    // renderselect($("#c-transaction_type_ids"), tran);
                    // renderselect($("#c-channel_ids"), channels);
                    return false;
                });
                return false;
            });
            Controller.api.bindevent();
        },
        edit: function () {
            require(['bootstrap-select', 'bootstrap-select-lang']);
            var renderselect = function (select, data) {
                var html = [];
                for (var i = 0; i < data.length; i++) {
                    html.push("<option value='" + data[i].id + "'>" + data[i].name + "</option>");
                }
                $(select).html(html.join(""));
                select.trigger("change");
                if (select.data("selectpicker")) {
                    select.selectpicker('refresh');
                }
                return select;
            };

            var renderselected = function (select, data,selected) {
                var html = [];
                for (var i = 0; i < data.length; i++) {
                    if(selected.indexOf(data[i].id) != -1){
                        html.push("<option selected value='" + data[i].id + "'>" + data[i].name + "</option>");
                    }else{
                        html.push("<option value='" + data[i].id + "'>" + data[i].name + "</option>");
                    }
                }
                $(select).html(html.join(""));
                select.trigger("change");
                if (select.data("selectpicker")) {
                    select.selectpicker('refresh');
                }
                return select;
            };

            $(document).ready(function () {
                var product_id = $("#c-product_id").val();
                var rate_selected = String($("#c-rate_ids").data("data"));
                var card_selected = String($("#c-card_type").data("data"));
                var trans_selected = String($("#c-transaction_type_ids").data("data"));

                Fast.api.ajax({
                    url: "profit/list",
                    data: {product_id: product_id},
                }, function (data, ret) {
                    var rate = data.rate;
                    var card_type = data.card_type;
                    var tran = data.transaction_type;
                    $("#relation-zone .relation-item").remove();
                    renderselected($("#c-rate_ids"), rate,rate_selected);
                    renderselected($("#c-card_type"), card_type,card_selected);
                    renderselected($("#c-transaction_type_ids"), tran,trans_selected);
                    return false;
                });
                return false;
            });

            $(document).on('change', "#c-product_id", function () {
                var product_id = $(this).val();
                if(product_id == ""){
                    return false;
                }
                Fast.api.ajax({
                    url: "profit/list",
                    data: {product_id: product_id},
                }, function (data, ret) {
                    var rate = data.rate;
                    var card_type = data.card_type;
                    var tran = data.transaction_type;
                    $("#relation-zone .relation-item").remove();
                    renderselect($("#c-rate_ids"), rate);
                    renderselect($("#c-card_type"), card_type);
                    renderselect($("#c-transaction_type_ids"), tran);
                    return false;
                });
                return false;
            });
            Controller.api.bindevent();
        },
        setting:function(){
            $("#patch").click(function () {
                let arr = [];
                $("input[type=checkbox]:checked").each(function () {
                    arr.push($(this).val());
                });
                if(arr.length == 0){
                    Toastr.error("请选择数据！");
                    return false;
                }
                Layer.prompt({title: __('请输入数值'), formType: 0}, function (value, index){
                    $.each(arr,function (index,item) {
                        $("#"+item).val(value);
                    })
                    Layer.close(index);
                });
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
